from abc import ABC, abstractmethod
import requests
# 设置一个抽象类，继承自ABC，包含两个抽象方法：get_url和call_back，分别用于获取URL和回调函数。
class AbstractClass(ABC):

    def get_url(self):
        pass

    def call_back(self,code):
        pass

# 设置一个具体类，继承自AbstractClass，实现了两个抽象方法。

class DingDing(AbstractClass):
    def __init__(self):
        self.params = [
            "redirect_uri=http://localhost:5173/sflogin",
            "response_type=code",
            "client_id=ding2t4tjfshfqrsylb9",
            "scope=openid",
            "prompt=consent"
        ]


    def get_url(self):
        url = 'https://login.dingtalk.com/oauth2/auth?'+('&'.join(self.params))
        return url

    def call_back(self,code):
        data = {
            "grantType": "authorization_code",
            "clientId": "ding2t4tjfshfqrsylb9",
            "clientSecret": "3tqQQ9sA5IIwSGsRsECmn84GfA1Cba1d7NT5vCFIVDn7XhPgIsVZ6CmPO22VoyZJ",
            "code": code
        }

        resp = requests.post('https://api.dingtalk.com/v1.0/oauth2/userAccessToken', json=data).json()
        access_token = resp.get('accessToken')

        hearders = {'x-acs-dingtalk-access-token': access_token}
        resp = requests.get('https://api.dingtalk.com/v1.0/contact/users/me', headers=hearders).json()

        return resp,access_token








class WeChat(AbstractClass):

    def get_url(self):
        pass

    def call_back(self):
        pass


class Factory():
    def create_f(self,type):
        if type == 0:
            return WeChat()
        elif type == 1:
            return DingDing()

f = Factory()
